﻿using System;
using System.Collections.Generic;
using System.IO;

namespace Felbot.Profiles {
    /// <summary>
    /// This static class allows you to manage profiles and their settings.
    /// </summary>
    public static class ProfileControl {
        private static List<Profile> m_ProfileList;
        private static Profile m_Global;

        /// <summary>
        /// A list of the profile objects currently loaded.
        /// </summary>
        public static List<Profile> ProfileList {
            get {
                return m_ProfileList;
            }
        }

        /// <summary>
        /// A list of the profile objects currently open.
        /// </summary>
        public static List<Profile> GetOpenProfiles {
            get {
                List<Profile> profiles = new List<Profile>(ProfileList.Count);
                foreach (Profile profile in ProfileList) {
                    if (profile.Open) {
                        profiles.Add(profile);
                    }
                }
                profiles.TrimExcess();
                return profiles;
            }
        }

        /// <summary>
        /// The global profile.
        /// </summary>
        public static Profile Global {
            get {
                return m_Global;
            }
        }

        /// <summary>
        /// Initializes the profiles. Loads them from file.
        /// </summary>
        public static void Initialize() {
            m_ProfileList = new List<Profile>();
            string[] files = Directory.GetFiles(Program.ApplicationFolder, "*.fbp");
            FileInfo file;
            Profile profile;
            string profileName;
            foreach (string fileName in files) {
                file = new FileInfo(fileName);
                profileName = file.Name.Substring(0, file.Name.Length - file.Extension.Length);
                profile = MakeProfile(profileName);
            }
            if (m_Global == null) {
                m_Global = MakeGlobalProfile();
            }
        }

        /// <summary>Called to iterate through the profiles and start any profiles selected to start with Felbot.</summary>
        /// <param name="main">The main form.</param>
        public static void LoadOnFelbotStart(frmMain main) {
            foreach (Profile profile in ProfileList) {
                if (profile.GetSetting("General", "LoadOnFelbotStart").ToBoolean(false)) {
                    main.OpenProfile(profile.ProfileName);
                }
            }
        }

        /// <summary>
        /// Creates a global profile.
        /// </summary>
        /// <returns>Returns a global profile.</returns>
        private static Profile MakeGlobalProfile() {
            return MakeProfile("GLOBAL");
        }

        /// <summary>
        /// Creates a profile with the given name.
        /// </summary>
        /// <param name="a_sProfileName">The name to give this new profile.</param>
        /// <returns>Returns a new profile.</returns>
        public static Profile MakeProfile(string profileName) {
            if (GetProfile(profileName) == null) {
                Profile profile = new Profile(profileName, Global);
                if (!profileName.Equals("global", StringComparison.CurrentCultureIgnoreCase))
                    ProfileList.Add(profile);
                return profile;
            } else {
                return null;
            }
        }

        /// <summary>
        /// Gets a string representing a free "NewProfile#" profile to make a new profile by the name of.
        /// </summary>
        /// <returns>Returns a string representing a number.</returns>
        public static string GetMakeName {
            get {
                int l = 1;
                while (GetProfile("NewProfile" + l.ToString()) != null) {
                    l++;
                }
                return "NewProfile" + l.ToString();
            }
        }

        /// <summary>
        /// Gets the loaded profile.
        /// </summary>
        /// <param name="a_sName">The name of the profile to look for.</param>
        /// <returns>Returns the profile, or null if not found.</returns>
        public static Profile GetProfile(string name) {
            try {
                for (int i = 0; i < ProfileList.Count; i++) {
                    if (ProfileList[i].ProfileName.Equals(name, StringComparison.CurrentCultureIgnoreCase)) {
                        return ProfileList[i];
                    }
                }
            } catch (NullReferenceException) { }
            return null;
        }

        /// <summary>
        /// Removes the profile.
        /// </summary>
        /// <param name="a_profProfile">The name of the profile to look for.</param>
        public static void RemoveProfile(Profile profile) {
            profile.Delete();
            ProfileList.Remove(profile);
        }
    }
}
